<?

require_once("include.php");

$me=false;
require_once("header.session.php");
require_once("header.gui.php");
require_once("header.misc.php");
require_once("header.habits.php");

//globals
$MenuObjects= $me->getMyMenu();

if(!$me) {
	$me=&new Person($_SESSION['id']);
}

if(isset($_POST['action'])) {
	switch($_POST['action']) {
		case "writenewassessment":
			$ass=getAssessmentTypeInfo($_POST['typeid']);
			$data=array();
			foreach($ass['forms'] as $form) {
				$data[$form['field_name']]=$_POST[$form['field_name']];
			}
			$data['name']=$_POST['assessment_name'];
			$levels=getLevels();
			$habits=getHabits();
			$i=0;
			foreach($habits as $habit=>$ind_a) {
				$j=0;
				foreach($ind_a as $val) {
					if($_POST["habit" . $i . "ind" . $j]!=-1) {
						$data['habits'][$i][$j]=$_POST["habit" . $i . "ind" . $j];
					}
					$j++;
				}
				$i++;
			}
			if($me->isInGroup("Students")) {
				$personid=$me->id;
			} else {
				$personid=$_POST['personid'];
			}
			insertAssessmentData($_POST['typeid'],$personid,$data);
			break;
		case "edit":
			header("Location: " . $_SERVER['PHP_SELF'] . "?page=write&assessment_id=".$_POST['assessment']);
			die;
			break;
		case "editassessment":
			if(!canAccessAssessment($me->id,$_POST['assid'])) {
				break;
			}
			if($me->isInGroup("Students")) {
				$assinfo=getAssessment($_POST['assid']);
				if($assinfo['status']==100) {
					break;
				}
			}

			$ass=getAssessmentTypeInfo($_POST['typeid']);
			$data=array();
			foreach($ass['forms'] as $form) {
				$data[$form['field_name']]=$_POST[$form['field_name']];
			}
			$data['name']=$_POST['assessment_name'];
			$levels=getLevels();
			$habits=getHabits();
			$i=0;
			foreach($habits as $habit=>$ind_a) {
				$j=0;
				foreach($ind_a as $val) {
					if($_POST["habit" . $i . "ind" . $j]!=-1) {
						$data['habits'][$i][$j]=$_POST["habit" . $i . "ind" . $j];
					}
					$j++;
				}
				$i++;
			}
// 			var_dump($data);
			updateAssessment($_POST['assid'],$data);
			break;
		case "createnew":
			header("Location: " . $_SERVER['PHP_SELF'] . "?page=write&personid=".$_POST['studentid']);
			die;
			break;
	}
	header("Location: " . $_SERVER['PHP_SELF']);
	die;
}

if(!isset($_GET['page'])) {
	$_GET['page']="default";
}

function headers() {
	
}

function Body() {
	switch($_GET['page']) {
		case "view":
			require_once("view_assessments.php");
			break;
		case "write":
			echo "
<div id=\"desc\">
<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\" name=\"write\">
					";
			global $me;
			require_once("write_assessments.php");
			break;
		default:
?>
Welcome to the assessment center.
<br />
<?
global $me;
if($me->isInGroup("Students")) {
  echo "Here you can write new self assessments and view the assessments the staff members have written about you. You can also edit the assessments your have written if they have not yet been approved. Please note that all assessments must be approved by your Advisor, once an Assessment is approved it cannot be edited anymore.";
$asslist=getStudentAssessments($me->id);
// var_dump($asslist);=
echo "
<br><br>
Select an active assessment, or check out the <a href=\"assessment_archive.php\">archive</a> for assessments that are already in place.
<br>
<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\">
<input type=\"hidden\" name=\"action\" value=\"edit\">
<select name=\"assessment\">
";
foreach($asslist as $ass) {
	echo "
	<option value=\"" . $ass['assessment_id'] . "\">" . stripslashes($ass['assessment_name']) . "</option>";
}
echo "
</select>&nbsp;<input type=\"submit\" value=\"Edit Assessment\">
<br />
or <a href=\"assessments.php?page=write&assessment_type=1\">click here</a> to add a new assessment.
<br />
";


} elseif($me->isInGroup("Advisors")) {
  echo "Here you can write assessments for students, view or edit previous assessments, and approve student written assessments. Once an assessment is approved it should not be edited, though the option is there in case it's ever needed. Refrain from using that ability, for the sake of consistancy towards students.";
  echo "Currently only editing student assessments is supported.";
	$studentlist=getStudentList();
	echo "
			<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\">
			<input type=\"hidden\" name=\"action\" value=\"edit\">
			<select name=\"studentid\" id=\"studentid\">
			";
	foreach($studentlist as $student) {
		echo "
				<option value=\"" . $student['people_id'] . "\">" . stripslashes($student['people_firstname']) . " " . stripslashes($student['people_lastname']) . "</option>";
	}
	echo "
	</select>
	<select name=\"assessment\" id=\"assessment\">
	</select>
	<input type=\"submit\" value=\"Edit Assessment\">
	";

echo "
<script>
<!--
window.onload = function() {
	var thing = document.getElementById(\"studentid\");
	normalHeight=document.getElementById(\"mainDiv\").offsetHeight;
	thing.onchange = function() {
		eraseBox();
		";
		foreach($studentlist as $student) {
			echo "if(this[this.selectedIndex].value==".$student['people_id'].") {\n";
			$asslist=getStudentAssessments($student['people_id']);
			foreach($asslist as $ass) {
				echo "addToList(\"" . $ass['assessment_id'] . "\",\"" . $ass['assessment_name'] . "\");\n";
			}
			echo "}\n";
		}
		echo "
	}
}

function eraseBox() {
	var box=document.getElementById(\"assessment\");
	while(box.length!=0) {
		box.remove(0);
	}
}

function addToList(id,name)
{
	var elOptNew = document.createElement('option');
	elOptNew.text = name;
	elOptNew.value = id;
	var elSel = document.getElementById('assessment');
	try {
		elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		elSel.add(elOptNew); // IE only
	}
}


-->
</script>
</form>
	<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\">
	<input type=\"hidden\" name=\"action\" value=\"createnew\">
	<select name=\"studentid\" id=\"studentid\">
	";
	foreach($studentlist as $student) {
		echo "
		<option value=\"" . $student['people_id'] . "\">" . stripslashes($student['people_firstname']) . " " . stripslashes($student['people_lastname']) . "</option>";
	}
	echo "
	</select>
	<input type=\"submit\" value=\"Write New Assessment\">
	";
	
} elseif($me->isInGroup("Staff")) {
  echo "Here you can write assessments about students. You may also view/edit previously written assessments. Please note that all assessments must be approved by an Advisor, once an Assessment is approved it cannot be edited anymore.";
}

echo "
<!--
SOON TO COME ON THIS PAGE:
<br><br>
Approval System:
<br>
All assessments must be approved by an advisor.
<br>
Habits: Once approved the habits assessed in the assessment take effect on the student's overall habit \"snapshot\". EOQ's use the habit \"snapshot\" at the due date for assessments.
<br>
Checklists: For a checklist item to be checked off, the assessment must first be approved. Once approved the EK items are individually approved by the applicable staff member. Staff members who are qualified in a given field are the ONLY ones who can check off an item in that field. This is to meet a legal requirement.
Viewing assessments:
<br />
Students: All their assessments will be in a box with a scroll bar organized by date. Unapproved assessments written by staff/advisors are not visible.
<br />
Staff/Advisors: All students will be in one box, once a student is selected all their assessments will appear in the other box organized by date.
<br />
<br />
Writting Assessments:
<br />
Students: The different assessment types will be in a drop down box with a \"Write assessment\" button.
<br />
Staff/Advisors: All students will be in one box, all assessment types (non-self assessments) will be in another. Once both are selected the user clicks \"Write assessment\"... Assessments written by Staff must be approved by an advisor, Advisors can approve their own assessments.
-->";
			break;
	}
}

require_once("template.php");